package service

import (
	"perfect-vue-admin/global"
	"perfect-vue-admin/model"
	"perfect-vue-admin/model/request"
)

// @title    CreateSysOperationRecord
// @description   create a SysOperationRecord
// @param     sysOperationRecord               model.SysOperationRecord
// @auth                     （2020/04/05  20:22）
// @return    err             error

func CreateSysOperationRecord(sysOperationRecord model.SysOperationRecord) (err error) {
	err = global.DB.Create(&sysOperationRecord).Error
	return err
}

// @title    DeleteSysOperationRecord
// @description   delete SysOperationRecords
// @auth                     （2020/04/05  20:22）
// @param     sysOperationRecord               request.IdsReq
// @return                    error

func DeleteSysOperationRecordByIds(ids request.IdsReq) (err error) {
	err = global.DB.Unscoped().Delete(&[]model.SysOperationRecord{}, "id in (?)", ids.Ids).Error
	return err
}

// @title    DeleteSysOperationRecord
// @description   delete a SysOperationRecord
// @auth                     （2020/04/05  20:22）
// @param     sysOperationRecord               model.SysOperationRecord
// @return                    error

func DeleteSysOperationRecord(sysOperationRecord model.SysOperationRecord) (err error) {
	err = global.DB.Unscoped().Delete(sysOperationRecord).Error
	return err
}

func CleanSysOperationRecord() (error)  {
	return global.DB.Where("1 = 1").Delete(&model.SysOperationRecord{}).Error
}

// @title    GetSysOperationRecord
// @description   get the info of a SysOperationRecord
// @auth                     （2020/04/05  20:22）
// @param     id              uint
// @return                    error
// @return    SysOperationRecord        SysOperationRecord

func GetSysOperationRecord(id uint) (err error, sysOperationRecord model.SysOperationRecord) {
	err = global.DB.Where("id = ?", id).First(&sysOperationRecord).Error
	return
}

func GetSysOperationRecordsByIds(ids []int) (records []model.SysOperationRecord, err error) {
	err = global.DB.Find(&records, ids).Error
	return
}

// @title    GetSysOperationRecordInfoList
// @description   get SysOperationRecord list by pagination, 分页获取用户列表
// @auth                     （2020/04/05  20:22）
// @param     info            PageInfo
// @return                    error

func GetSysOperationRecordInfoList(info request.SysOperationRecordSearch) (err error, list interface{}, total int64) {
	// 创建db
	table := global.DB.Model(&model.SysOperationRecord{})
	var sysOperationRecords []model.SysOperationRecord
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.Username != "" {
		table = table.Where("username LIKE ?", "%" +info.Username + "%")
	}
	if info.Method != "" {
		table = table.Where("method = ?", info.Method)
	}
	if info.Path != "" {
		table = table.Where("path = ?", info.Path)
		//table = table.Where("path LIKE ?", "%"+info.Path+"%")
	}
	if info.Desc != "" {
		table = table.Where("`desc` = ?", info.Desc)
	}
	if info.Status != 0 {
		table = table.Where("status = ?", info.Status)
	}
	if len(info.DateRange) == 2 {
		table = table.Where("created_at BETWEEN ? AND ?", info.DateRange[0], info.DateRange[1])
	}
	if info.SortColumn != "" && info.Order != "" {
		table = table.Order(info.SortColumn + " " + info.Order)
	}
	table.Count(&total)
	if info.Page == -1 && info.PageSize == -1 {
		err = table.Order("id desc").Find(&sysOperationRecords).Error
	}else {
		err = table.Order("id desc").Limit(info.PageSize).Offset(info.PageSize * (info.Page - 1)).Find(&sysOperationRecords).Error
	}
	return err, sysOperationRecords, total
}
